﻿///////////////////////////////////////////////////////////////////////////////////////////////
// System.Text.Parsing - © 2011 Sebastien Pellizzari
// Released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
///////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections.Generic;
using System.Collections;

namespace System.Text.Parsing
{
    /// <summary>Represents a collection of grammar properties.</summary>
    public sealed class GrammarPropertyCollection : IEnumerable<GrammarProperty>
    {
        private readonly Dictionary<string, GrammarProperty> _elements;

        //-----------------------------------------------------------------------
        //     Properties
        //-----------------------------------------------------------------------

        /// <summary>Gets the number of elements in the collection.</summary>
        public int Count { get { return _elements.Count; } }

        /// <summary>Gets the property object with specified name, or a null reference if not found.</summary>
        /// <param name="name">The property name.</param>
        public GrammarProperty this[string name]
        {
            get
            {
                GrammarProperty property;
                if (_elements.TryGetValue(name, out property)) return property;
                return null;
            }
        }

        //-----------------------------------------------------------------------
        //     Constructors
        //-----------------------------------------------------------------------

        /// <summary>Initializes an instance of the <see cref="GrammarPropertyCollection"/> class.</summary>
        public GrammarPropertyCollection() { _elements = new Dictionary<string, GrammarProperty>(); }

        //-----------------------------------------------------------------------
        //     Methods
        //-----------------------------------------------------------------------

        /// <summary>Determines whether the collection contains a property with specified name.</summary>
        /// <param name="name">The property name.</param>
        public bool Contains(string name)
        {
            return _elements.ContainsKey(name);
        }

        /// <summary>Adds an element to the collection.</summary>
        public void Add(GrammarProperty property)
        {
            if (property == null) throw new ArgumentNullException("property");
            _elements[property.Name.Text] = property;
        }

        /// <summary>Removes the property with specified name from the collection.</summary>
        /// <param name="name">The name of the property to remove.</param>
        /// <returns>true if succeeded; false otherwise.</returns>
        public bool Remove(string name) { return _elements.Remove(name); }

        /// <summary>Returns an object that iterates through the collection.</summary>
        /// <returns>An object that implements the <see cref="IEnumerator{GrammarProperty}"/> interface.</returns>
        public IEnumerator<GrammarProperty> GetEnumerator() { return _elements.Values.GetEnumerator(); }

        //-----------------------------------------------------------------------
        //     Non Public
        //-----------------------------------------------------------------------

        // GetEnumerator method.
        IEnumerator IEnumerable.GetEnumerator() { return _elements.Values.GetEnumerator(); }
    }
}
